/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import oracle.sql.CLOB;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.lob.SerializableClob;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.File;
import java.io.Writer;
import java.sql.Clob;
import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.tech15.core.model.Attach;
import com.tech15.core.service.AttachManager;
import com.tech15.pub.model.*;
import com.tech15.pub.dao.*;
import com.tech15.pub.service.*;
import com.tech15.pub.vo.query.*;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PubTemplateManager extends BaseManager<PubTemplate,java.lang.String>{

	private PubTemplateDao pubTemplateDao;
	private AttachManager attachManager;
	private PubDataDao pubDataDao;
	
	public void setPubDataDao(PubDataDao pubDataDao) {
		this.pubDataDao = pubDataDao;
	}
	public void setAttachManager(AttachManager attachManager) {
		this.attachManager = attachManager;
	}
	public void setPubTemplateDao(PubTemplateDao dao) {
		this.pubTemplateDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.pubTemplateDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PubTemplateQuery query) {
		return pubTemplateDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return pubTemplateDao.findByPageRequest(pr);
	}
	
	public PubTemplate getById(String id){
		PubTemplate pubTemplate = pubTemplateDao.getById(id);
		if(ObjectUtils.isNotEmpty(pubTemplate.getZt())){
			PubData pd = pubDataDao.getById(pubTemplate.getZt());
			if(pd != null){
				pubTemplate.setDataname(pd.getName());
			}
		}
		if(ObjectUtils.isNotEmpty(pubTemplate.getLb())){
			PubData pd = pubDataDao.getById(pubTemplate.getLb());
			if(pd != null){
				pubTemplate.setCategoryName(pd.getName());
			}
		}
		List<Attach> attach = attachManager.findByTableNameAndId(Notes.TABLE_PUB_TEMPLATE, ""+id);
		pubTemplate.setAttach(attach);
		return pubTemplate;
	}
	public void saveTemplate(PubTemplate pubTemplate, String realpath, File attachFile, String attachFileFileName, String attachFileContentType) {
		pubTemplate.setUptime(DateTools.getCurrentTime());
		pubTemplateDao.save(pubTemplate);
		pubTemplateDao.flush();
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttach(realpath, pubTemplate.getUserid(), Notes.TABLE_PUB_TEMPLATE, ""+pubTemplate.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
	}
	
	public void updateTemplate(PubTemplate pubTemplate, String realpath, File attachFile, String attachFileFileName, String attachFileContentType) {
		pubTemplate.setUptime(DateTools.getCurrentTime());
		pubTemplateDao.update(pubTemplate);
		pubTemplateDao.flush();
		
		if(attachFile != null  && attachFileFileName != null){
			attachManager.delete(Notes.TABLE_PUB_TEMPLATE, pubTemplate.getId());
			attachManager.saveAttach(realpath, pubTemplate.getUserid(), Notes.TABLE_PUB_TEMPLATE, ""+pubTemplate.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
	}
	
}
